package hotel.quartz.oversea.config;

import hotel.base.oversea.utils.HttpClientBaseAuth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
*@Description: restTemplate 拦截器 统一配置token
*@Author: Zhang·jz
*@date: 2020/8/4
*/

@Component
@Slf4j
public class TokenInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
            throws IOException {
        log.info("进入RestTemplate拦截器");
        String accessToken = HttpClientBaseAuth.getAuthTokenByAppname("sec-quartz");
        log.info("accessToken -->: {}",accessToken);
        HttpHeaders headers = request.getHeaders();
        headers.add("Authorization", "bearer  "+ accessToken);
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        return execution.execute(request, body);
    }
}
